package com.stark.wechat.job;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.stark.wechat.service.impl.JobQueueServiceImpl;

/**
 * 每日报名作业。
 * <p>解决多台服务器负载均衡时定时任务重复执行的问题：每天各台服务器先报名，最先报名的服务器执行定时任务。
 * @author Ben
 * @since 1.0.0
 * @version 1.0.1, 2017/9/22
 */
@Component("pushJobQueueDailyJob")
public class PushJobQueueDailyJob {
	Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	private JobQueueServiceImpl jobQueueService;
	
	public void execute() {
		try {
			int times = this.jobQueueService.countToday(PushTemplateMessageJob.JOB_ID);
			if (times == 0) {
				this.jobQueueService.add(PushTemplateMessageJob.JOB_ID, PushTemplateMessageJob.JOB_NAME);
				logger.info("成功报名作业：{}", PushTemplateMessageJob.JOB_NAME);
			}
		} catch (Exception e) {
			logger.error("报名作业时出错：JOB_ID={},JOB_NAME={}",
					PushTemplateMessageJob.JOB_ID, PushTemplateMessageJob.JOB_NAME, e);
		}
	}
}
